﻿Public Class frmProgress
    ' Public variables allow our progress to be written from the main thread
    ' which will happen every time I fire an event.
    Public Event timerFire()
    Public candidatePercent As Double
    Public gemPercent As Double
    Public candidateDone As Boolean
    Public gemDone As Boolean
    Public totalcandidateTicks As Integer
    Public totalgemticks As Integer
    Public nogemsfound As Boolean
    Public nocandidatesfound As Boolean

    Public Event allDone()
    Public Event cancelCalc()
    Public Event errorDone()

    Public Sub progStart()
        ' Do the startup events for this form
        ' when a calculation starts.
        Try
            Me.Show()
            tmrProgress.Enabled = True
            totalcandidateTicks = 0
            totalgemticks = 0
            tmrProgress.Interval = 1000 ' 1 second intervals
            ' Set everything visible / invisible as needed.
            lblGemming.Visible = False
            lblGemmingTime.Visible = False
            prgGemming.Visible = False

            lblFindCandidates.Text = "Finding Candidate Gearsets - 0%"
            lblFindCandidatesTime.Text = "Calculating..."
            prgCandidates.Value = 0

            candidatePercent = 0
            gemPercent = 0
            candidateDone = False
            gemDone = False
            nogemsfound = False
            nocandidatesfound = False
        Catch ex As Exception
            MessageBox.Show("An unexpected error has occurred. DK Optimize will exit.", "DK Optimize", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
            ErrLogWriter.writeLog(ex)
            End
        End Try

    End Sub

    Private Sub tmrProgress_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrProgress.Tick
        ' Update our stuff.
        'Try
        RaiseEvent timerFire()

        ' Display everything.
        ' Error block catches percents not yet being initialized.
        Try
            prgCandidates.Value = System.Math.Min(candidatePercent, 100)
            prgGemming.Value = System.Math.Min(gemPercent, 100)
        Catch ex As Exception
        End Try

        If candidateDone = True Then
            lblFindCandidates.Text = "Finding Candidate Gearsets - 100%"
            lblFindCandidatesTime.Text = "Done!"
            lblGemming.Visible = True
            lblGemmingTime.Visible = True
            prgGemming.Visible = True
            lblGemming.Text = "Gemming Candidate Sets - " & System.Math.Round(gemPercent, 3) & "%"
            totalgemticks += 1
            lblGemmingTime.Text = timeLeft(gemPercent, totalgemticks)
        Else
            ' Increment our ticks data, etc.
            totalcandidateTicks += 1
            lblFindCandidatesTime.Text = timeLeft(candidatePercent, totalcandidateTicks)
            lblFindCandidates.Text = "Finding Candidate Gearsets - " & System.Math.Round(candidatePercent, 3) & "%"
        End If

        If gemDone = True Then
            Me.Dispose()
            RaiseEvent allDone()
        End If
        If nocandidatesfound = True Then
            tmrProgress.Enabled = False
            MessageBox.Show("No valid candidate sets could be found.  Please try again with different settings.", "DK Optimize", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
            Me.Dispose()
            RaiseEvent errorDone()
        End If
        If nogemsfound = True Then
            tmrProgress.Enabled = False
            MessageBox.Show("No valid gemmed sets could be found. Please try again with different settings.", "DK Optimize", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
            Me.Dispose()
            RaiseEvent errorDone()
        End If
        'Catch ex As Exception
        '    MessageBox.Show("An unexpected error has occurred. DK Optimize will exit.", "DK Optimize", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
        '    ErrLogWriter.writeLog(ex)
        '    End
        'End Try

    End Sub

    Private Function timeLeft(ByVal percentDone As Double, ByVal secondsElapsed As Integer) As String
        ' Returns a nice formatted string.
        Try
            Dim answer As String
            Dim secondsLeft As Double
            Dim hours As Integer, minutes As Integer, seconds As Integer

            answer = "Time Remaining: "

            If secondsElapsed < 10 Or percentDone = 0 Then
                timeLeft = answer & "Calculating"
                Exit Function
            End If

            secondsLeft = (secondsElapsed * (100 / percentDone)) - secondsElapsed

            hours = System.Math.Floor(secondsLeft / 3600)
            secondsLeft = secondsLeft Mod 3600
            minutes = System.Math.Floor(secondsLeft / 60)
            secondsLeft = secondsLeft Mod 60
            seconds = secondsLeft

            If hours > 0 Then
                If hours = 1 Then
                    answer &= hours & " hour"
                Else
                    answer &= hours & " hours"
                End If
                If minutes > 0 Then
                    If minutes = 1 Then
                        answer &= " " & minutes & " minute"
                    Else
                        answer &= " " & minutes & " minutes"
                    End If
                End If
            Else
                If minutes > 0 Then
                    If minutes = 1 Then
                        answer &= minutes & " minute"
                    Else
                        answer &= minutes & " minutes"
                    End If
                    If seconds > 0 Then
                        If seconds = 1 Then
                            answer &= " " & seconds & " second"
                        Else
                            answer &= " " & seconds & " seconds"
                        End If
                    End If
                Else
                    If seconds = 1 Then
                        answer &= seconds & " second"
                    Else
                        answer &= seconds & " seconds"
                    End If
                End If
            End If
            timeLeft = answer
        Catch ex As Exception
            MessageBox.Show("An unexpected error has occurred. DK Optimize will exit.", "DK Optimize", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
            ErrLogWriter.writeLog(ex)
            End
        End Try
    End Function

    Private Sub btnCancelDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelDone.Click
        Try
            Me.Close()
        Catch ex As Exception
            MessageBox.Show("An unexpected error has occurred. DK Optimize will exit.", "DK Optimize", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
            ErrLogWriter.writeLog(ex)
            End
        End Try
    End Sub

    Private Sub frmProgress_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        ' Determine if we want to cancel.  If we do, close this form and send our cancel events etc.
        Try
            Dim msgResult As DialogResult

            msgResult = MessageBox.Show("Do you really want to cancel the calculation?", "DK Optimize", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

            If msgResult = Windows.Forms.DialogResult.No Then
                e.Cancel = True
            Else
                RaiseEvent cancelCalc()
                e.Cancel = False
            End If
        Catch ex As Exception
            MessageBox.Show("An unexpected error has occurred. DK Optimize will exit.", "DK Optimize", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
            ErrLogWriter.writeLog(ex)
            End
        End Try
    End Sub
End Class